//
//  PluginController.m
//  Maciago
//
//  Created by Miguel Marcos on Sat Jun 15 2002.
//  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
//

#import "PluginController.h"

@interface PluginController (ProtectedMethods)
-_init;
+alloc;
@end

@implementation PluginController

+ alloc
{
    NSLog (@"Warning: Calling History alloc, use sharedInstance instead");
    return [self sharedInstance];
}

- _init
{
    pluginName = [[NSString stringWithString: @"default"] retain];
    pluginPath = [[[NSBundle mainBundle] builtInPlugInsPath] retain];
	//NSLog (@"%@", pluginPath);
    pluginType = [[NSString stringWithString: @"mext"] retain];
    defaultTitle = [[NSString stringWithString: @"default"] retain];

    buffer = [[NSMutableDictionary alloc] init];
    return self;
}

+sharedInstance
{
    static PluginController *instance = nil;

    if (instance == nil)
    {
        //	NSLog (@"Calling PluginController sharedInstance (1st time)");
        instance = [[super alloc] _init];
    }
    //    else
    //	NSLog (@"Calling PluginController sharedInstance (>1 time)");

    return instance;
}

-(void)dealloc
{
    [super dealloc];
}

- (id)imageNamed:(NSString *)imageName
{
    NSImage *image = [buffer objectForKey:imageName];
    NSString *path = [NSString stringWithFormat:@"%@/%@.%@/%@.png",
        pluginPath, pluginName, pluginType, imageName];

    if (image == nil)
    {
//        NSLog (@"%@", path);
        image = [[NSImage alloc] initWithContentsOfFile:path];

        if (image == nil)
            image = [NSImage imageNamed:imageName];

        [image setDataRetained:YES];
        [buffer setObject:image forKey:imageName];
    }

    return image;
}

- (id)objectNamed:(NSString *)objectName
{
    NSImage *object = [buffer objectForKey:objectName];
    NSString *path = [NSString stringWithFormat:@"%@/%@.%@/%@",
        pluginPath, pluginName, pluginType, objectName];

    if (object == nil)
    {
        object = [[NSData alloc] initWithContentsOfFile:path];

        if (object != nil)
            [buffer setObject:object forKey:objectName];
    }

    return object;
}


- (NSArray *)scanPluginsIn:(NSString *)path ofType:(NSString *)type
{
    NSMutableArray *direnum;
    NSMutableArray *pluginsList = [[NSMutableArray alloc] init];
    int i;

    [pluginsList addObject:defaultTitle];
    [pluginType release];
//    [pluginPath release];
    pluginType = [type retain];
//    pluginPath = [path retain];
    direnum = [[[NSFileManager defaultManager]
                    directoryContentsAtPath:pluginPath] retain];

    if (direnum != nil)
    {
        for (i = 0; i < [direnum count]; ++i)
        {
            if ([[[direnum objectAtIndex:i] pathExtension] isEqualToString:type])
			{
                [pluginsList addObject:[[direnum objectAtIndex:i] 
                                    stringByDeletingPathExtension]];
			//NSLog(@"plugin: %@/n", [[direnum objectAtIndex:i] 
            //                        stringByDeletingPathExtension]);
			}
        }

        [direnum release];
    }
    
    [pluginsList autorelease];    
    return pluginsList;
}

-(void)setCurrentPlugin:(NSString *)plugin
{
    NSString *path = [NSString stringWithFormat:@"%@/%@.%@/plugin-info.plist",
        pluginPath, plugin, pluginType];

    if ([[NSFileManager defaultManager] fileExistsAtPath: path])
    {
        [plugin retain];
        
        if (![pluginName isEqualToString:plugin])
        {
                [pluginName release];
                pluginName = [plugin retain];
                [buffer setDictionary:
                    [NSDictionary dictionaryWithContentsOfFile: path]];
        }
        [plugin release];
    }
    else
    {
        pluginName = [defaultTitle retain];
        [buffer removeAllObjects];     
    }    
}

-(NSString *)currentPlugin
{
    return pluginName;
}

-(void)setDefaultTitle:(NSString *)title;
{
    if (title != nil)
    {
        [defaultTitle release];
        defaultTitle = [title retain];
    }
}

-(void)reset
{
    [buffer removeAllObjects];
}

@end
